package com.wzqj.agent.util;

import java.util.Hashtable;

import org.apache.commons.io.output.ByteArrayOutputStream;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class CreateQRCodeKit {
	
	/**
     * 生成二维码
     *
     * @param content 待编码内容
     * @param width   二维码宽度
     * @param height  二维码高度
     * @param format  图片保存格式  png / jpg / bmp
     * @throws Exception
     */
	public static ByteArrayOutputStream createQr(String content, int width, int height, String format) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToStream(bitMatrix, format, baos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return baos;
    }


}
